package com.zhang.cart.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.zhang.cart.service.ICartService;
import com.zhang.cart.vo.CartItem;
import com.zhang.cart.vo.CartVo;
import com.zhang.common.util.redis.RedisUtil;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.entity.SkuInfo;
import com.zhang.goods.entity.SkuSaleAttrValue;
import com.zhang.goods.service.ISkuInfoService;
import com.zhang.goods.service.ISkuSaleAttrValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CartServiceImpl implements ICartService {
    private final String CART_PREFIX = "cart:";//redis中保存的前缀
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ISkuInfoService skuInfoService;
    @Autowired
    private ISkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public R addGoodsToCart(Long skuId, Integer num) {
        long userId = StpUtil.getLoginIdAsLong();
        String cartKey = CART_PREFIX + userId;
        /*如果已经存在，在数量上叠加*/
        if (redisUtil.hget(cartKey, skuId.toString()) != null) {
            String jsonStr = String.valueOf(redisUtil.hget(cartKey, skuId.toString()));
            CartItem cartItem = JSON.parseObject(jsonStr, CartItem.class);
            cartItem.setCount(cartItem.getCount() + num);
            String jsonString = JSON.toJSONString(cartItem);
            log.info("序列化:-->{}", jsonString);
            redisUtil.hset(cartKey, skuId.toString(), jsonString, 604800L);
        } else {
            CartItem cartItem = new CartItem();
            /*查询商品详细信息*/
            SkuInfo skuInfo = skuInfoService.getById(skuId);
            cartItem.setSkuId(skuId);
            cartItem.setTitle(skuInfo.getSkuTitle());
            /*暂时选择图片id*/
            cartItem.setImage(skuInfo.getSkuDefaultImg().toString());
            cartItem.setPrice(skuInfo.getPrice());
            cartItem.setCount(num);
            /*查询销售属性值*/
            List<SkuSaleAttrValue> skuSaleAttrValues = skuSaleAttrValueService.listBySkuId(skuId);
            List<String> collect = skuSaleAttrValues.stream().map(SkuSaleAttrValue::getAttrValue).collect(Collectors.toList());
            cartItem.setSkuAttr(collect);
            String jsonString = JSON.toJSONString(cartItem);
            /*保存并设置时间(hash结构)*/
            /*7天*/
            redisUtil.hset(cartKey, skuId.toString(), jsonString, 604800L);
        }

        return RUtils.successMsg("添加成功!");
    }

    @Override
    public CartVo listByUserId(Long userId) {
        String cartKey = CART_PREFIX + userId;
        Map<Object, Object> hmget = redisUtil.hmget(cartKey);
        ArrayList<String> arrayList = new ArrayList(hmget.values());
        List<CartItem> collect = arrayList.stream().map(item -> {
            return JSON.parseObject(item, CartItem.class);
        }).collect(Collectors.toList());
        CartVo cartVo = new CartVo();
        cartVo.setItems(collect);
        cartVo.setCountType(arrayList.size());//有多少种商品
        return cartVo;
    }

    @Override
    public void delListByUserIdAndSkuIds(Long userId, List<String> skuIds) {
        String key = CART_PREFIX + userId;
        redisUtil.hdel(key, skuIds.toArray(new String[0]));
    }

}
